using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

namespace Terra.Core.Middleware.Authentication
{
    /// <summary>
    /// JWT认证中间件
    /// </summary>
    public class JwtAuthenticationMiddleware
    {
        private readonly RequestDelegate _next;

        public JwtAuthenticationMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task InvokeAsync(HttpContext context)
        {
            // 从请求头获取Token
            var token = context.Request.Headers["Authorization"].ToString()?.Replace("Bearer ", "");

            if (!string.IsNullOrEmpty(token))
            {
                // TODO: 验证Token
                // TODO: 设置用户身份信息
            }

            await _next(context);
        }
    }
}